package rtu;

import java.util.Arrays;

import com.serotonin.io.serial.SerialParameters;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse;

import entity.ConnectionInfor;

public class re_3 {
	private static String commPortId;
	private static int baudRate; 
	private static int dataBits;
	private static int stopBits;
	private static int parity;
	private static int slaveId;
	private static int start;
	private static int len;
	
//	public re_3()
//	{

		
//		 commPortId=conninfor.getHatchway_name();
//		 baudRate =Integer.parseInt( conninfor.getBaud_rate()); 
//		 dataBits = Integer.parseInt( conninfor.getData_bits());
//		 stopBits = Integer.parseInt( conninfor.getStop_bit());
//		 parity = Integer.parseInt(conninfor.getOdd_even_check());
//		 slaveId = conninfor.getModel_address();
//		 start = conninfor.getRegister_address();
//		 len  = conninfor.getRegister_number();
		 
//		 System.out.println("--commPortId:"+commPortId+"  "+"slaveId:"+slaveId+"  "+"start:"+start+"  "+"len:"+len+"----");
//	};
	public static int[] remain1(ConnectionInfor conninfor){
		   //String result = null; 
		 commPortId=conninfor.getHatchway_name();
		 baudRate =Integer.parseInt( conninfor.getBaud_rate()); 
		 dataBits = Integer.parseInt( conninfor.getData_bits());
		 stopBits = Integer.parseInt( conninfor.getStop_bit());
		 parity = Integer.parseInt(conninfor.getOdd_even_check());
		 slaveId = conninfor.getModel_address();
		 start = conninfor.getRegister_address();
		 len  = conninfor.getRegister_number();
//		
		  short result[] = null;
		SerialParameters serialParameters = serialParaSet(commPortId, baudRate, dataBits,  stopBits,  parity);
		ModbusFactory modbusFactory = new ModbusFactory();

        ModbusMaster master = modbusFactory.createRtuMaster(serialParameters);
       
        try {
			master.init();
		} catch (ModbusInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       result = readHoldingRegistersTest(master, slaveId, start, len);
      int resultint[] = new int[result.length];
       for(int i=0; i<result.length; i++)
          {
    	   resultint[i] =(int)result[i]; 
    	   }
       try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
            master.destroy(); 
            try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
            return resultint;
        
	};
	
	
	
	private static SerialParameters serialParaSet(String commPortId3, int baudRate3, int dataBits3, int stopBits3, int parity3) {
		SerialParameters serialParameters = new SerialParameters();		
		serialParameters.setCommPortId(commPortId3);//
		serialParameters.setParity(parity3);//	
		serialParameters.setDataBits(dataBits3);//	
		serialParameters.setStopBits(stopBits3);//
		serialParameters.setPortOwnerName(commPortId3);		
		serialParameters.setBaudRate(baudRate3);//
		return serialParameters;
	}
	public static short[] readHoldingRegistersTest(ModbusMaster master, int slaveId3, int start3, int len3) {
        short[] result1 = null; 
	  
		try {
            ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest(slaveId3, start3, len3);
            ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse) master.send(request);

            if (response.isException())
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            else
            	result1 = response.getShortData();
                System.out.println(Arrays.toString(response.getShortData()));
            	
        }
        catch (ModbusTransportException e) {
            e.printStackTrace();
        }
        return result1;
    }

}
